
demolitionOrders();
function demolitionOrders(){
	var demolitionOrdertable="";

    var itemsDetail=[];
    var skuDetail={};
    skuDetail = {
        "skuCode": "1245", //skuCode
        "skuNum": 5, //skuNum

    }
    itemsDetail.push(skuDetail)


	$.ajax({
        type: "POST",
        url: root.dockingplatform.splitOrder,
        cache: false, //禁用缓存
        dataType: "json",
        contentType: "application/json;charset=UTF-8",

        data:JSON.stringify({
            orderId: sessionStorage.getItem("demolitionOrderId"),
            // "mayArrivedIn":$("#logmin").val(),
            operateUserId: JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
            operateUserName: JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
            orderItemsDetail: itemsDetail
        }),

		success: function (result) {

			/*//收货人信息
            var listAddressReceiverarr=[]
            var listAddressReceivers=""
            if(result.data.listAddressReceiver!=""||result.data.listAddressReceiver!=null){

                var listAddressReceiverall=result.data.listAddressReceiver
                for(var k=0;k<listAddressReceiverall.length;k++) {

                    var information= listAddressReceiverall[k].receiver_name+'&nbsp'+listAddressReceiverall[k].mobile
                    listAddressReceiverarr.push(information);
                }
                for(var j = 0; j < listAddressReceiverarr.length; j++) {
                    if(j < listAddressReceiverarr.length - 1) {
                        listAddressReceivers += listAddressReceiverarr[j] + "、";
                    } else {
                        listAddressReceivers += listAddressReceiverarr[j] + "";
                    }
                }
                $("#listAddressReceiver").html(listAddressReceivers)

            }
*/
			if (result.errorCode=="0"){

                $("#order_ids").html(sessionStorage.getItem("demolitionOrderId"));
                $("#receiveDetailAddress").html(sessionStorage.getItem("receiveDetailAddress"));

                var listAddressReceivers = sessionStorage.getItem("listAddressReceivers")
                $("#listAddressReceiver").html(listAddressReceivers)
                //表格
                var arr=result.data;
                $.each(arr,function(k,v){

                    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))
                    var warehouse="";
                    $.each(warehouses.data,function(k,s){
                        if(sessionStorage.getItem("parent_warehouse_id")==s.warehouseCode){
                            warehouse=s.warehouseName
                        }
                    })
                    //单价
                    var price=v.channelSettledPrice.toFixed(2);
                    /*var specialPrice=""
                    if(v.activity_type == 10){
                        specialPrice=v.promotion_value/v.buyyer_count
                    }
                    price=(price-specialPrice).toFixed(2)*/
                    //表格
                    //表格
                    if(v.occupyVirtualStore >0){

                        demolitionOrdertable += '<tr  bgcolor="#ffb6c1 ">' +
                        '                            <td class="demolitionOrder_1"><div style="cursor:pointer;"><img title="" style="float: left;width: 100px;height: 100px;" name="" src="'+v.productPic+'" alt=""><div style="float: left;width:180px;padding-top: 20px;margin-left: 10px;"><div>'+v.skuCode+'</div><div>'+v.productName+'</div><div>'+v.color+'</div><div>'+v.skuInfo+'</div></div></div></td>' +
                        '                            <td class="demolitionOrder_2">'+warehouse+'</td>' +
                        '                            <td class="demolitionOrder_3"><input type="text" class="input-text mr-5" oninput="divisibleQuantity(this,'+v.canSplitNum+')" style="width: 43px;" value=""></td>' +
                        '                            <td class="demolitionOrder_4">'+(v.buyerCount-v.canSplitNum)+'</td>' +
                        '                            <td class="demolitionOrder_5">' + v.buyerCount + '' + v.unitName + '</td>' +
                        '                            <td class="demolitionOrder_6">' + v.actualStock + '</td>' +
                        '                            <td class="demolitionOrder_7">' + price + '元/' + v.unitName + '</td>' +
                        '                            <td class="demolitionOrder_8" style="display: none">' + v.skuCode + '</td>' +
                            '                            <td class="demolitionOrder_9" style="display: none">' + v.buyerCount + '</td>'+
                        '                    </tr>'

                    }else {
                        demolitionOrdertable += '<tr>' +
                            '                            <td class="demolitionOrder_1"><div style="cursor:pointer;"><img title="" style="float: left;width: 100px;height: 100px;" name="" src="'+v.productPic+'" alt=""><div style="float: left;width:180px;padding-top: 20px;margin-left: 10px;"><div>'+v.skuCode+'</div><div>'+v.productName+'</div><div>'+v.color+'</div><div>'+v.skuInfo+'</div></div></div></td>' +
                            '                            <td class="demolitionOrder_2">'+warehouse+'</td>' +
                            '                            <td class="demolitionOrder_3"><input type="text" class="input-text mr-5" oninput="divisibleQuantity(this,'+v.canSplitNum+')" style="width: 43px;" value=""></td>' +
                            '                            <td class="demolitionOrder_4">'+(v.buyerCount-v.canSplitNum)+'</td>' +
                            '                            <td class="demolitionOrder_5">' + v.buyerCount + '' + v.unitName + '</td>' +
                            '                            <td class="demolitionOrder_6">' + v.actualStock + '</td>' +
                            '                            <td class="demolitionOrder_7">' + price + '元/' + v.unitName + '</td>' +
                            '                            <td class="demolitionOrder_8" style="display: none">' + v.skuCode + '</td>' +
                            '                            <td class="demolitionOrder_9" style="display: none">' + v.buyerCount + '</td>'+
                            '                    </tr>'
                    }
                })


                $("#datatable").find("tbody").html($(demolitionOrdertable));
			} else {
                layer.msg(result.msg, {icon: 5,time:2000});
			}

		},
        error: function (result) {
            $.Huimodalalert(result.msg,2000);
        }
	});
}
//可拆分数量效验
function divisibleQuantity(val,canSplitNum) {
    console.log("val;[]",canSplitNum);
    console.log("Num",Number($(val).val()));
    //需要<=购买数量-已拆分数量的总和。如果超出，不允许录入
    if(Number($(val).val())>Number(canSplitNum)){
        $(val).val("");
        $.Huimodalalert('输入数值错误',1000);

    }
    //"整数"
    //(只能输入数字)
    val.value = val.value.replace(/[^\d]/g,""); //清除"数字"和"."以外的字符
    val.value = val.value.replace(/^\./g,""); //验证第一个字符是数字
    /*val.value=val.value.replace(/[^1-9-]+/,'');*/
    if($(val).val()!=""){
        if(Number($(val).val())<=0){
            $(val).val("")
            $.Huimodalalert('输入数值错误',2000);
        }
    }
}


//刷新实际库存
function refresh() {
    var itemsDetail=[];
    var skuDetail={};
    skuDetail = {
        "skuCode": "1245", //skuCode
        "skuNum": 5, //skuNum

    }
    itemsDetail.push(skuDetail)


    $.ajax({
        type: "POST",
        url: root.dockingplatform.splitOrder,
        cache: false, //禁用缓存
        dataType: "json",
        contentType: "application/json;charset=UTF-8",

        data:JSON.stringify({
            orderId: sessionStorage.getItem("demolitionOrderId"),
            // "mayArrivedIn":$("#logmin").val(),
            operateUserId: JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
            operateUserName: JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
            orderItemsDetail: itemsDetail
        }),
		success: function (result) {
            if (result.errorCode==0){
                $.Huimodalalert("刷新成功",2000);
                var productList = $('#datatable tr');
                for (var j = 0; j < result.data.length; j++) {
                    console.log("====");
                    console.log(productList[j].find('.demolitionOrder_6 ').value);
                    console.log(result.data[j].actualStock);
                    $(productList[j]).find('.demolitionOrder_6 ').text( result.data[j].actualStock)
                    /*for (var i = 0; i < productList.length; i++) {

                    }*/
                }
            } else {
                $.Huimodalalert(result.msg,2000);
            }

		},
	});
}

//生成子订单
var isclickbtn = true;
function generate() {
	var productList = $('#datatable tr');
	var itemsDetail=[]
	var skuDetail={}
	for(var j=0;j<productList.length;j++) {
		if($(productList[j]).find('.demolitionOrder_3 input').val()!=""&&$(productList[j]).find('.demolitionOrder_3 input').val()!=undefined){
            skuDetail = {
				"skuCode": $(productList[j]).find('.demolitionOrder_8').text(), //skuCode
				"skuNum": $(productList[j]).find('.demolitionOrder_3 input').val(), //skuNum

			}
            itemsDetail.push(skuDetail)
		}
	}
   /* var createSubOrder = {
        "orderId":sessionStorage.getItem("demolitionOrderId"),
        // "mayArrivedIn":$("#logmin").val(),
        "operateUserId": JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
        "operateUserName":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
        "itemsDetail":itemsDetail
    };*/
    if(isclickbtn){
        isclickbtn =false
    $.ajax({
        type: "POST",
        url: root.dockingplatform.createSubOrder,
        cache: false, //禁用缓存
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data:JSON.stringify({
            orderId: sessionStorage.getItem("demolitionOrderId"),
            // "mayArrivedIn":$("#logmin").val(),
            operateUserId: JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
            operateUserName: JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
            orderItemsDetail: itemsDetail,
            operatorCode:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.number

        }),

        success: function (result) {
            if (result.errorCode==0){
                $.Huimodalalert("生成子订单成功",2000);
                /*var productList = $('#datatable tr');
                for (var j = 0; j < result.data.length; j++) {
                    console.log("====");
                    console.log(productList[j].find('.demolitionOrder_6 ').value);
                    console.log(result.data[j].actualStock);
                    $(productList[j]).find('.demolitionOrder_6 ').text( result.data[j].actualStock);
                    $(productList[j]).find('.demolitionOrder_4 ').text(result.data[j].buyerCount-result.data[j].canSplitNum)
                    /!*for (var i = 0; i < productList.length; i++) {

                    }*!/
                }*/
                demolitionOrders()
            } else{
                $.Huimodalalert(result.msg,2000);
              
            }
            isclickbtn = true
        },
    });
}
// setTimeout(function () {
//     isclickbtn = true
//   },500)
}


//自动计算
function calculation() {
    var datasList = $('#datas tr');
    var demolitionOrderval=""
    datasList.each(function (index,dom) {
        demolitionOrderval=$(dom).find('td.demolitionOrder_9').html()-$(dom).find('td.demolitionOrder_4').html()
        $(dom).find('td.demolitionOrder_3 input').val(demolitionOrderval)
    })
}